// 底部
if (window.plus) {
    plusReady();

} else {
    document.addEventListener('plusready', plusReady, false);
}

// 初始化
function plusReady() {
    var subpages = ['index.html', 'classification.html', '../coin/coinlist.html', '../shopcart/shoppingcart.html', '../mine/mylife.html'];
    var subpage_style = {top: '0px', bottom: '50px'};
    var aniShow = {}, haveInitPages = [];
    //当前激活选项
    var activeTab = subpages[0];

    var self = plus.webview.currentWebview();
    for (var i = 0; i < 5; i++) {
        var temp = {};
        var sub = plus.webview.create(subpages[i], subpages[i], subpage_style);
        if (i > 0) {
            sub.hide();
        } else {
            haveInitPages.push(subpages[0]);
            temp[subpages[i]] = "true";
            $.extend(aniShow, temp);
        }
        self.append(sub);
    }


    // 绑定事件
    $("#footer").on("tap", "li", function () {
        var targetTab = $(this).attr("tip");
        if (targetTab == activeTab) {
            return;
        }

        // 选中样式
        var activeli = $('#footer li[class=color-fff]');
        activeli.removeClass("color-fff");
        $(this).addClass("color-fff");

        // 第一次点击时候初始化页面
        if (haveInitPages.indexOf(targetTab) == -1) {
            haveInitPages.push(targetTab);
            plus.webview.getWebviewById(targetTab).evalJS("ready()");
        }

        //显示目标选项卡
        if (os.ios || aniShow[targetTab]) {
            plus.webview.show(targetTab);
        } else {
            var temp = {};
            temp[targetTab] = "true";
            $.extend(aniShow, temp);
            plus.webview.show(targetTab, "none", 300);
        }
        //隐藏当前;
        plus.webview.hide(activeTab);
        //更改当前活跃的选项卡
        activeTab = targetTab;
    });
}

/**
 * 返回主页关闭其他页
 * */
function goHome() {
    var viewlist = plus.webview.all();
    var exceptlist = ["main.html"];
    for (var i = 0; i < viewlist.length; i++) {
        var webview = allwebview[i];
        if (exceptlist.indexOf(webview.id) > -1) {
            // TODO
        } else {
            webview.close("none");
        }
    }
}
